<?php
namespace app\api\controller;

class Company extends Permission
{
    public function getList()
    {
        $page = isset($this->input_data['page']) ? $this->input_data['page'] : 1;
        $limit = isset($this->input_data['limit']) ? $this->input_data['limit'] : 15;
        $map = [];

        if(isset($this->input_data['title']) && $this->input_data['title'] !== '') $map['title'] = ['LIKE','%'.$this->input_data['title'].'%'];
        if(isset($this->input_data['area_id']) && $this->input_data['area_id'] != 0) $map['area_id'] = $this->input_data['area_id'];

        if($this->post('area_id') > 0)
        {
            $map['area_id'] = $this->post('area_id');
        }
        else
        {
            $map['area_id'] = ['IN',$this->myAllAreaIds];
        }


        $total = db('Company')->where($map)->count();

        $rows = db('Company')->order('id', 'desc')->page($page,$limit)->where($map)->select();


        $list = [];

        foreach ($rows as $v) {
            $list[] = $v;
        }

        
        $data = [
            'total' => $total,
            'list' => $list,
            'map' => $map
        ];

        $this->resSuc($data);
    }
    public function getMyList()
    {
        $adminArea = db('AdminArea')->alias('a')->join('company_area b','a.area_id = b.id','LEFT')->where(['admin_id'=>$this->user['id']])->order('sort ASC,id DESC')->select();
        $myAreaIds = [];

        
        foreach ($adminArea as $v) {
            $myAreaIds[] = $v['id'];
        }
        

        $map = [
            'area_id' => ['IN',$myAreaIds]
        ];
        $list = db('Company')->order('id', 'desc')->where($map)->select();


        $this->resSuc($list);
    }

    public function update()
    {
        $data = [];

        $data['title'] = $this->input_data['title'];
        $data['area'] = $this->input_data['area'];
        $data['area_id'] = $this->input_data['area_id'];

        if($this->input_data['id'] > 0)
        {
            db('Company')->where(['id'=>$this->input_data['id']])->update($data);
        }
        else
        {
            $id = db('Company')->insertGetId($data);
        }

        $this->resSuc('ok');
    }

    public function del()
    {
        db('Company')->delete( $this->input_data['id']);
        
        $this->resSuc('ok');
    }

    public function getProfit()
    {
        # 收费（车辆出单的总金额）
        
    }

}
